package com.rainman.mybatisplus.joinparam;


/**
 * 连表重要参数jp
 *
 *
 * @author lqy
 */
public class JoinParam {
    private String asName;
    private StringBuilder selectStr;
    private StringBuilder str;//="left join tableb b on a.id=b.aid";


    /**
     * 去除不取的字段 默认取a所有的字段 - exSelect
     */
    private StringBuilder exSelect;


    public JoinParam(){
        this("a");
    }

    public JoinParam(String baseTableAsName){
        this.asName=baseTableAsName;

        selectStr=new StringBuilder();
        exSelect=new StringBuilder();
        str=new StringBuilder();

        selectStr.append(",");
        exSelect.append(",");
    }

    /**
     * 基准表要去除的字段，
     * *为去除基准表全部
     * id为去除基准表的id字段
     *
     *
     * @param name
     * @return
     */
    public JoinParam exBaseTableSelect(String... name){

        for(String s:name){
            exSelect.append(s).append(",");
        }
        return this;
    }

    /**
     * 要查出的字段
     * 不建议用 某某表.*
     * 使用比如a.id as aid
     *  b.id
     *  a.name
     *  b.name as bname
     *
     *
     * @param select
     * @return
     */
    public JoinParam select(String... select){
        for(String s:select){
            selectStr.append(s).append(",");
        }

        return this;
    }

    /**
     * 左连接
     *
     * @param tableName 要连接的表名
     * @param asName 要连接的表名的别名
     * @param on 连接条件，可以多个 比如 b.aid=a.id 与 b.deleted=false 可链接为 on b.aid=a.id and b.deleted=false
     * @return
     */
    public JoinParam leftJoin(String tableName,String asName, String... on){

        str.append(" left join ").append(tableName).append(" ").append(asName);

        String headStr=" on ";

        for(String o:on){
            str.append(headStr).append(o);
            headStr=" and ";
        }
        return this;
    }

    /**
     * 右连接
     *
     * @param tableName 要连接的表名
     * @param asName 要连接的表名的别名
     * @param on 连接条件，可以多个 比如 b.aid=a.id 与 b.deleted=false 可链接为 on b.aid=a.id and b.deleted=false
     * @return
     */
    public JoinParam rightJoin(String tableName,String asName, String... on){
        str.append(" right join ").append(tableName).append(" ").append(asName);

        String headStr=" on ";
        for(String o:on){
            str.append(headStr).append(o);
            headStr=" and ";
        }
        return this;
    }

    public JoinParam join(String tableName,String asName, String... on){
        str.append(" join ").append(tableName).append(" ").append(asName);

        String headStr=" on ";

        for(String o:on){
            str.append(headStr).append(o);
            headStr=" and ";
        }
        return this;
    }


    public String getFinalJoinStr(){
        return str.toString();
    }

    public String getAsName() {
        return asName.trim();
    }

    public String getSelectStr() {
        return selectStr.toString().trim();
    }

    public String getExSelect() {
        return exSelect.toString().trim();
    }




    /*好像没有支持 SFunction的必要
    public JoinParam select(SFunction<?, ?>... select){
        for(SFunction<?, ?> s:select){
            selectStr+=s+",";
        }

        return this;
    }*/

}
